<?php
/**
 * 后台用户管理控制器
 *
 * @author liang<lang@vip.deyi.com>
 * @since   2016/11/29 10:47
 * @copyright  deyi.com
 */

namespace Apps\Acp\Controllers;

use Apps\Acp\Models\Manager;
use Apps\Acp\Models\ManagerGroup;
use Apps\Common\Constant\BusinessConstant;
use Apps\Common\Controller\AcpBaseController;
use Deyi\Functions\PageFunction;
use Deyi\Functions\UtilFunction;

/**
 * Class ManagerController
 * @package Apps\Acp\Controllers
 */
class ManagerController extends AcpBaseController
{
    public function indexAction()
    {
        $page = PageFunction::getPageAndOffset($this->getParamValue('page', 1, 'int'), BusinessConstant::PAGE_SIZE);
        $name = $this->getParamValue('name', '');
        $conditions = [];

        if ($name) {
            $conditions['username%'] = $name;
        }

        $total = Manager::getInstance()->countListByConds($conditions);
        $list = [];
        $groupNames = [];
        if ($total > 0) {
            $list = Manager::getInstance()->getListByConds($conditions, $page['offset'], $page['size']);
            $gids = [];

            foreach ($list as $v) {
                $gids[] = $v->groupid;
            }
            $groupNames = ManagerGroup::getInstance()->getGroupNamesByGids($gids);
        }

        $this->view->setVars([
            'list' => $list,
            'groupnames' => $groupNames,
            'pageHtml' => PageFunction::getPageHtml(
                $page['page'],
                $total,
                $page['size'],
                parse_url($this->request->getURI())['path'],
                $conditions,
                4
            )
        ]);
    }

    public function addUserAction()
    {

        $groupList = ManagerGroup::getInstance()->getListByConds([], 0, BusinessConstant::MAX_DATA_SIZE);

        $this->view->setVars([
            'grouplist' => $groupList,
            'data' => null
        ]);
    }


    public function editUserAction()
    {
        $id = $this->getParamValue('id', null, 'int');

        $data = Manager::getInstance()->getOneByField('id', $id);

        if (!$data) {
            static::errorThrow('对应用户数据不存在');
        }

        $this->view->pick('Manager/Adduser');

        $groupList = ManagerGroup::getInstance()->getListByConds([], 0, BusinessConstant::MAX_DATA_SIZE);

        $this->view->setVars([
            'grouplist' => $groupList,
            'data' => $data,
        ]);
    }

    public function switchAction()
    {
        $id = $this->getParamValue('id', null, 'int');
        $enable = $this->getParamValue('enable', 1, 'int');

        Manager::getInstance()->updateByFields(
            ['enable' => $enable],
            ['id' => $id]
        );

        parent::renderSuccessJson();
    }

    public function saveUserAction()
    {
        $id = $this->getParamValue('id', 0, 'int');
        $userName = $this->getParamValue('username');
        $passWord = $this->getParamValue('password', '');
        $phone = $this->getParamValue('phone');
        $intro = $this->getParamValue('intro');
        $groupId = $this->getParamValue('groupid', null, 'int');
        $busId = $this->getParamValue('busid', 0, 'int');
        //检查是否有重名
        $tmp = Manager::getInstance()->getOneByField('username', $userName);

        if ($tmp && ((int)$tmp->id != $id)) {
            static::errorThrow('存在相同用户名数据');
        }

        if (!empty($passWord) && mb_strlen($passWord) < 8) {
            static::errorThrow('密码不能小于8位长度');
        }

        $values = [
            'username' => $userName,
            'phone' => $phone,
            'groupid' => $groupId,
            'busid' => $busId,
            'intro' => $intro,
            'dateline' => time(),
        ];

        if ($id) {

            if (!empty($passWord)) {
                $salt = UtilFunction::getRandomString(8);
                $values['password'] = md5(md5($passWord) . $salt);
                $values['salt'] = $salt;
            }

            Manager::getInstance()->updateByFields($values, ['id' => $id]);
        } else {

            if (empty($passWord)) {
                static::errorThrow('密码不能为空');
            }

            $salt = UtilFunction::getRandomString(8);
            $values['password'] = md5(md5($passWord) . $salt);
            $values['salt'] = $salt;

            $values['enable'] = 1;
            Manager::getInstance()->insertByFields($values);
        }

        parent::renderSuccessJson();
    }

    public function deleteUserAction()
    {
        $id = $this->getParamValue('id', null, 'int');

        Manager::getInstance()->deleteByField('id', $id);

        parent::renderSuccessJson();
    }
}